package com.pulaien.shop.controller;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.pulaien.common.annotation.Log;
import com.pulaien.common.constant.HttpStatus;
import com.pulaien.common.core.controller.BaseController;
import com.pulaien.common.core.domain.AjaxResult;
import com.pulaien.common.core.domain.model.LoginUser;
import com.pulaien.common.core.page.TableDataInfo;
import com.pulaien.common.enums.BusinessType;
import com.pulaien.common.utils.ConstantsUtil;
import com.pulaien.common.utils.SecurityUtils;
import com.pulaien.common.utils.poi.ExcelUtil;
import com.pulaien.system.domain.ConfigAddressInfo;
import com.pulaien.system.domain.dto.ExtConfigAddressInfo;
import com.pulaien.system.service.IConfigAddressInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 收件地址-发货地址配置Controller
 *
 * @author pulaien
 */
@RestController
@RequestMapping("/shop/configAddressInfo")
@Api(tags = "商城端收件地址-发货地址配置")
public class ShopAddressInfoController extends BaseController
{
    @Autowired
    private IConfigAddressInfoService configAddressInfoService;

    @ApiOperation("新增和编辑")
    @RequestMapping(value = "/insertAndEdit", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult insertAndEdit(@RequestBody ConfigAddressInfo company) {
        return configAddressInfoService.insertAndEdit(company);
    }

    @ApiOperation(value = "列表分页展示")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request,ExtConfigAddressInfo company) {
        String lang  = request.getHeader("Accept-Language");
        startPage();
        List<ExtConfigAddressInfo> record=configAddressInfoService.list(company);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setTotal(new PageInfo(record).getTotal());
        for(ExtConfigAddressInfo info:record){
            info.setProvinceName(info.getProvinceName());
            info.setCityName(info.getCityName());
            info.setAreaName(info.getAreaName());
        }
        rspData.setRows(record);
        return rspData;
    }


    @ApiOperation(value = "收件地址列表无分页")
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult getList(ExtConfigAddressInfo company) {
        List<ExtConfigAddressInfo> record=configAddressInfoService.list(company);
        return AjaxResult.success(record);
    }


    @ApiOperation(value = "地址管理-地址下拉选项")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前记录起始索引", paramType = "query", defaultValue = "1", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", paramType = "query", defaultValue = "10", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "orderByColumn", value = "排序列", paramType = "query"),
            @ApiImplicitParam(name = "isAsc", value = "排序的方向 \"desc\" 或者 \"asc\"", paramType = "query"),
            @ApiImplicitParam(name = "reasonable", value = "分页参数合理化", paramType = "query", dataType = "Boolean")
    })
    @GetMapping(value = "/getAllOrderList")
    public TableDataInfo getAllOrderList(
            ExtConfigAddressInfo company
    ) {
        startPage();
        Long userId = SecurityUtils.getUserId();
        company.setUserId(userId);
        List<ConfigAddressInfo> record = configAddressInfoService.getAllOrderList(company);
        return getDataTable(record);
    }
}
